<script setup>

import approveApi from '../api/approve.js'
import api from '../api/detail.js';
import dayjs from 'dayjs'

import { showToast } from 'vant';

// 获取查询字符串部分
const queryString = window.location.search.substring(1); // "name=John&age=30"
// 将查询字符串分割成键值对数组
const paramsArray = queryString.split('&');
// 创建一个对象来存储参数
const params = {};
paramsArray.forEach(param => {
  const [key, value] = param.split('=');
  params[decodeURIComponent(key)] = decodeURIComponent(value);
});

let autoId = params.autoId;
let flowType = params.flowType;
const userNameId = params.userNameId;
let item = {};
const list = ref([])

const active = ref('detail');
//const formData = reactive({});
const formData = ref([]);
const attachList = ref([]);

// 通过审批
function approve() {
  approveApi.approve(item, userNameId).then(function(res) {
    if(res.code === '200'){
      return window.location.href= '/system/approve/approve/index.html?userNameId='+userNameId;
    }else{
      let msg = res.message+":"+res.remark.message;
      console.log(msg);
      alert(msg);
    }
  });
}

function reject() {
  approveApi.reject(item, userNameId).then(function(res) {
    if(res.code === '200'){
      return window.location.href= '/system/approve/approve/index.html?userNameId='+userNameId;
    }else{
      let msg = res.message+":"+res.remark.message;
      console.log(msg);
      alert(msg);
    }
  });
}

function formatSize(value) {
  if(!value){
    return "0 Bytes";
  }
  var unitArr = new Array("Bytes","KB","MB","GB","TB","PB","EB","ZB","YB");
  var index=0;
  var srcsize = parseFloat(value);
  index=Math.floor(Math.log(srcsize)/Math.log(1024));
  var size =srcsize/Math.pow(1024,index);
  size=size.toFixed(2);//保留的小数位数
  return size+unitArr[index];
}

function getDetail() {
  api.getDetail(autoId,flowType).then((res)=> {
    Object.assign(item, res);
  });
  api.getDetailRows(autoId, flowType).then((res)=> {
    formData.value = res.rows;
  });
}
function getFileList() {
  api.getFileList(autoId).then((res)=> {
    attachList.value = res;
  });
}

function getAttachViewUrl(item) {
  if(item.fileType === 'pdf'){
    //return '/dcm/system/attachment/download?uuid=' + item.uuid
    window.location.href = '/dcm/erp/displayPDF/pdf?fileUrl=' + encodeURIComponent(item.filePath);
  } else {
    showToast('无法预览的文件');
    //window.location.href = item.filePath;
  }

}

function back() {
  return window.location.href= '/system/approve/approve/index.html?userNameId='+userNameId;
}

onMounted(()=> {
  getDetail();
  getFileList();
})
</script>

<template>
  <div>
    <van-tabs v-model:active="active" type="card">
      <van-tab name="detail" title="详情">
        <div v-for="item in formData" class="item" style="padding: 1em">
          <div class="content">
              <div>
                <div v-if="item.flowType==='CGJH'">物项名称：{{ item.subName }}</div><div v-else>单位名称：{{ item.secondaryUnit }}</div>
              </div>
              <div>
                <div v-if="flowType==='CGJH'">所属机构：{{ item.secondaryUnit }}</div><div v-else>评审办法：{{ item.subName }}</div>
              </div>
              <div>
                <div v-if="flowType==='CGJH'">采购类型：{{ item.purchaseType }}</div><div v-else>是否中标：{{ item.purchaseType }}</div>
              </div>
              <div>
                金额：{{ item.amount }} 元
              </div>
              <div>
                申请人：{{ item.creator }}
              </div>
              <div>
                申请时间：{{ dayjs(item.createTime).format('YYYY-MM-DD') }}
              </div>
          </div>
        </div>
      </van-tab>
      <van-tab name="attach" title="附件">
          <div v-for="item in attachList" class="item" style="padding: 1em">
            <div class="content">
                <div>
                  附件名称：<a href='#' @click="getAttachViewUrl(item)">{{ item.fileName }}</a>
                </div>
                <div>
                  附件大小：{{ item.fileSize }}
                </div>
                <div>
                  上传人：{{ item.creator }}
                </div>
                <div>
                  上传时间：{{ dayjs(item.createTime).format('YYYY-MM-DD') }}
                </div>
            </div>
        </div>
      </van-tab>
    </van-tabs>

    <div class="bottom-action-bar">
      <van-button type="primary" size="small" @click="back">返回主页</van-button>
      <van-button type="primary" size="small" @click="approve">同意</van-button>
      <van-button  type="danger" size="small" @click="reject">退回</van-button>
    </div>
  </div>
</template>

<style scoped>
.item {
  border-radius: 4px;
  border: 1px solid #ddd;
  margin: 1em;
  overflow: hidden;
  height: 10em;
  display: flex;
  flex-direction: column;
  font-size: 1em;

}

.item .header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0.5em;
  border-bottom: 1px solid #ddd;

}

.item .header .buttons button+button {
  margin-left: 0.5em;
}
.item .content {
  padding: 0.5em;
}
</style>